package com.intelligentpension.kotlinwifi

import android.Manifest
import android.content.Context
import android.content.IntentFilter
import android.location.LocationManager
import android.net.wifi.WifiManager
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hjq.toast.ToastUtils
import com.intelligentpension.adapter.ItemAdapter
import com.intelligentpension.bean.WifiBean
import com.intelligentpension.receiver.WifiBroadcast
import com.intelligentpension.utils.WifiMangerUtil
import com.permissionx.guolindev.PermissionX


class MainActivity : AppCompatActivity(), WifiBroadcast.OnBroadListener {
    private lateinit var mManager: WifiMangerUtil
    private lateinit var mSearch: TextView
    private lateinit var mRecyclerView: RecyclerView
    private lateinit var mAdapter: ItemAdapter
    private var mList: MutableList<WifiBean> = ArrayList()
    private var mAdressList: MutableList<String> = ArrayList()
    private lateinit var filter: IntentFilter
    private var broadcast: WifiBroadcast? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mManager = WifiMangerUtil(this@MainActivity)

        mSearch = findViewById<TextView>(R.id.search_wifi)
        mRecyclerView = findViewById<RecyclerView>(R.id.recycler)
        var manager = LinearLayoutManager(this)
        mRecyclerView.layoutManager = manager
        mAdapter = ItemAdapter(R.layout.wifi_item, mList)
        mRecyclerView.adapter = mAdapter

        PermissionX.init(this@MainActivity).permissions(
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_FINE_LOCATION
        ).onForwardToSettings { _, _ ->
        }.request { _, _, _ ->
        }
        initData()
    }

    private fun initData() {

        if (mManager.isSupportedWifi()) {
            if (!mManager.openWifi()) {
                ToastUtils.show("wifi打开失败,请往手机系统打开")
            } else {
                var wifiList = mManager.getWifiList()
                for (scan in wifiList) {
                    var bssid = scan.BSSID
                    var ssid = scan.SSID
                    if (!mAdressList.contains(bssid)) {
                        mAdressList.add(bssid)
                        //TODO:设置名字和地址
                        if (ssid.isNullOrEmpty()) {
                            ssid = "未知"
                        }
                        var bean = WifiBean(ssid, bssid)
                        mList.add(bean)
                    }
                }
                mAdapter.notifyDataSetChanged()
            }
        }

        //开启广播
        filter = IntentFilter()
        filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION)
        filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)
        broadcast = WifiBroadcast(this@MainActivity)
        registerReceiver(broadcast, filter)

        var locationManger = getSystemService(Context.LOCATION_SERVICE) as LocationManager
        //GPS定位
        var gps = locationManger.isProviderEnabled(LocationManager.GPS_PROVIDER)
        var netWork = locationManger.isProviderEnabled(LocationManager.NETWORK_PROVIDER)

        //点击刷新扫描wifi
        mSearch.setOnClickListener {
            if (gps || netWork) {
                mManager.startScanWifi()
            }
        }

        //点击连接wifi
        mAdapter.setOnItemClickListener { _, _, position ->
            var bean = mList[position]
            var SSID = bean.name
            mManager.router(SSID)
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(broadcast)
    }

    //当wifi列表刷新的时候
    override fun onRefresh() {
        var wifiList = mManager.getWifiList()
        for (scan in wifiList) {
            var bssid = scan.BSSID
            var ssid = scan.SSID
            if (!mAdressList.contains(bssid)) {
                mAdressList.add(bssid)
                //TODO:设置名字和地址
                if (ssid.isNullOrEmpty()) {
                    ssid = "未知"
                }
                var bean = WifiBean(ssid, bssid)
                mList.add(bean)
            }
        }
        mAdapter.notifyDataSetChanged()
    }
}